/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.merashehar.servlet.admin;

import com.merashehar.db.util.ConnectionManager;
import com.merashehar.db.util.DatabaseHandler;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.SQLException;

/**
 *
 * @author binkam
 */
public class ItemServletAddition extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /*
             * TODO output your page here. You may use following sample code.
             */
            ConnectionManager connectionManager = new ConnectionManager();
            DatabaseHandler databaseHandler = new DatabaseHandler();
            Statement statement = connectionManager.getConnection().createStatement();
            ResultSet resultSet = databaseHandler.getResultSet("select * from address;", connectionManager.getConnection());
           

            String itemId = request.getParameter("itemId");
            String itemName = request.getParameter("itemName");
            String itemCity = request.getParameter("itemCity");
            String itemSubCategory = request.getParameter("itemSubCategory");
            String itemType = request.getParameter("itemType");
            String description = request.getParameter("description");
            String addressline1 = request.getParameter("addressline1");
            String addressline2 = request.getParameter("addressline2");
            
            String phone1 = request.getParameter("phone1");
            String phone2 = request.getParameter("phone2");
            String mobile = request.getParameter("mobile");
            String zip = request.getParameter("zip");
            String landmark1 = request.getParameter("landmark1");
            String landmark2 = request.getParameter("landmark2");
            out.println(itemCity);
            int i = statement.executeUpdate("insert into address values(2,addressline1,addressline2,NULL,phone1,phone2,mobile,zip,landmark1,landmark2)");


            
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet ItemServletAddition</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet ItemServletAddition at " + request.getContextPath() + "</h1>");
            out.println("<table>");
            out.println("<tr>");
            out.println("<td>");
            out.println("address_id");
            out.println("</td>");
            out.println("<td>");
            out.println("address_line1");
            out.println("</td>");
            out.println("<td>");
            out.println("address_line2");
            out.println("</td>");
            out.println("<td>");
            out.println("city");
            out.println("</td>");
            out.println("<td>");
            out.println("phone1");
            out.println("</td>");
            out.println("<td>");
            out.println("phone2");
            out.println("</td>");
            out.println("<td>");
            out.println("mobile");
            out.println("</td>");
            out.println("<td>");
            out.println("zip");
            out.println("</td>");
            out.println("<td>");
            out.println("landmark1");
            out.println("</td>");
            out.println("<td>");
            out.println("landmark2");
            out.println("</td>");
            out.println("</tr>");
            while(resultSet.next())
            {
                 out.println("<tr>");
                 out.println("<td>");
                 out.println(resultSet.getInt(1));
            out.println("</td>");
            out.println("<td>");
            out.println(resultSet.getString(2));
            out.println("</td>");
            out.println("<td>");
            out.println(resultSet.getString(3));
            out.println("</td>");
            out.println("<td>");
            out.println(resultSet.getString(4));
            out.println("</td>");
            out.println("<td>");
            out.println(resultSet.getInt(5));
            out.println("</td>");
            out.println("<td>");
            out.println(resultSet.getInt(6));
            out.println("</td>");
            out.println("<td>");
            out.println(resultSet.getInt(7));
            out.println("</td>");
            out.println("<td>");
            out.println(resultSet.getInt(8));
            out.println("</td>");
            out.println("<td>");
            out.println(resultSet.getString(9));
            out.println("</td>");
            out.println("<td>");
            out.println(resultSet.getString(10));
            out.println("</td>");
            out.println("</tr>");
                 
            }

            out.println("</table>");
            out.println("</body>");
            out.println("</html>");
        } catch (SQLException ex) {
            Logger.getLogger(DatabaseHandler.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
